import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Supermarket supermarket = Supermarket.loadData();
        if (supermarket == null) {
            System.out.print("请输入超市名称: ");
            String name = scanner.nextLine();
            System.out.print("请输入超市地址: ");
            String address = scanner.nextLine();
            supermarket = new Supermarket(name, address);
        }

        while (true) {
            System.out.println("\n1. 开卡");
            System.out.println("2. 查询会员");
            System.out.println("3. 查看所有会员");
            System.out.println("4. 退出");
            System.out.print("请选择操作: ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 清除换行符

            switch (choice) {
                case 1: // 开卡
                    System.out.print("请输入会员卡号: ");
                    String cardNumber = scanner.nextLine();
                    System.out.print("请输入会员姓名: ");
                    String name = scanner.nextLine();
                    supermarket.addMember(cardNumber, name);
                    break;

                case 2: // 查询会员
                    System.out.print("请输入会员卡号: ");
                    String searchCardNumber = scanner.nextLine();
                    Member member = supermarket.findMember(searchCardNumber);
                    if (member != null) {
                        System.out.println(member);
                    } else {
                        System.out.println("找不到该会员。");
                    }
                    break;

                case 3: // 查看所有会员
                    supermarket.showMembers();
                    break;

                case 4: // 退出
                    supermarket.saveData();
                    System.out.println("退出系统。");
                    return;

                default:
                    System.out.println("无效的选择，请重新输入。");
            }
        }
    }
}

